const bleTool = require("../../utils/bleTool");

// pages/equipmentList/equipmentList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    devices:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var BLEData = app.globalData.bleTool.getBLEData();


    // if(BLEData.connectedDevice != null) {
    //   if(BLEData.connectedDevice.localName == that.data.deviceLocalName){
    //     that.haveConnected();
    //     return;
    //   }
    // }
    // if(BLEData.BLEOpen == false){
    //   app.globalData.bleTool.openBluetoothAdapter();
    // }else {
    //     console.log("发现设备");
    //   app.globalData.bleTool.startBluetoothDevicesDiscovery();
    // }
  },

  search(){
    console.log("发现设备");
    app.globalData.bleTool.startBluetoothDevicesDiscovery();
    // wx.navigateBack();
  },

  //监听蓝牙状态
  BLEOpenStatus: function() {
    let that = this;
    var BLEData = app.globalData.bleTool.getBLEData();
    console.log("获取到了状态");
    if(BLEData.BLEOpen){
        if(app.globalData.bleTool.getBLEData.connectedDevice){
            app.globalData.bleTool.closeBLEConnection(null);
        }
        if(app.globalData.bleTool.getBLEData.connectedDevice == null){
            that.showLoad();
            app.globalData.bleTool.startBluetoothDevicesDiscovery();
        }
    }else {
      that.setData({
        loadModal: false
      })
      wx.showToast({
        title: '蓝牙已关闭,请先打开蓝牙',
        icon: 'none',
        duration: 3000
      })
    }
  },  

  //搜索到设备回调
  onSearchDevice: function () {
    let that = this;
    var BLEData = app.globalData.bleTool.getBLEData();
    var devices = BLEData.devices;
    that.setData({
        devices: devices
    })
    // for(var i = 0; i < devices.length; i++){
    //   var device = devices[i];
    //   if(device.localName == that.data.deviceLocalName){
        // //连接蓝牙
        // if(!that.data.isSync){
        //   that.setData({
        //     isSync: true
        //   })
        //   app.globalData.bleTool.createBLEConnection(device);
        //   return;
        // }
    //   }
    // }
  },

  connectDevice:function (e){
    var device = e.currentTarget.dataset.item;
    // console.log(e);
    app.globalData.bleTool.createBLEConnection(device);
    app.globalData.bleTool.stopBluetoothDevicesDiscovery();
  },
  
  //连接失败
  connectFail: function () {
    let that = this;
    that.setData({
      loadModal: false
    })
  },

  //连接成功回调
  haveConnected: function () {
    let that = this;
    //status:spd=0,err=1,spoiler=1,mode=0,m1=0,m2=0,m1max=0,m2max=0,vol=12.1
    console.log("我连接成功啦");
    that.setData({
      
    })
    var currentPages = getCurrentPages();
    var lastPage = currentPages[currentPages.length - 2];
    if (lastPage.haveConnected) {
        lastPage.haveConnected();
    }

    wx.navigateBack();
  },  

    //数据写入失败
    writeError: function (){
        console.log("我在做测试");
        let that = this;
        that.setData({
            loadingTitle: "正在搜索",
            showPercent: false,
            percent: 0,
            isSync: false,
            loadModal: false
        })
        wx.showToast({
          title: '蓝牙已断开，请重试',
          icon: 'none',
          duration: 3000
        });
    },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("我卸载啦")
    bleTool.stopBluetoothDevicesDiscovery()
    var BLEData = app.globalData.bleTool.getBLEData();
    BLEData.devices = Array()
    BLEData.deviceNames = Array()

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})